<?php

class History extends Controller {

	function History()
	{		
		parent::__construct();
				
	}
	
	function event($imonth,$iday,$id)
	{
		$this->load->model("history_model");
		$this->load->model("history_img_model");
		$this->load->helper('url');
		$id = intval($id);
		$info  = $this->history_model->getInfo($id);
		
		$month = $info->month;
		$day = $info->day;
		if(intval($imonth)!= intval($month) or intval($iday)!= intval($day))
		{
			return ;
		}
 		$year = intval(date("Y"));
		$data['month'] = $month;
		$data['day'] = $day;
		$data['prev_day'] = date("m/d",strtotime("$year-$month-$day")-86400); 
		$data['next_day'] = date("m/d",strtotime("$year-$month-$day")+86400); 		
		$data['calendar'] =  $this->get_calendar_html($year,$month,$day);
		$data['history'] = $info;
		$data['description'] = mb_substr("历史上的今天 ".$info->title . " " .$info->description,0, 300,"utf8");
		//print_r($info);
		
		$condition = "hid='$id'";
		$imgs = $this->history_img_model->getList($condition);
		$imglist = array();
		foreach($imgs as $row)
		{
			$temp = array();
			$temp['image']  = "/pic/$month/$day/".$row->filename;
			$temp['title'] = $row->title;
			$imglist[] = $temp;
		}	
		$data['images'] = $imglist;
		$this->load->view("event.html",$data);
	}
	
	function index($month=0,$day = 0)
	{
		$month=intval($month);
		$day = intval($day);
		$this->hlist($month,$day);
	}
	function hlist($month=0,$day=0)
	{
		$this->load->model("history_model");
		$this->load->model("history_img_model");
		
		$year = intval(date("Y"));
		if($month==0)	$month = intval(date("m"));
		if($day==0) $day = intval(date("d"));	
		$data['calendar'] =  $this->get_calendar_html($year,$month,$day);
		$condition = "month='$month' and day='$day'";
		$hlist = $this->history_model->getList($condition);
		$data['hlist'] = $hlist;
		$data['month'] = $month;
		$data['day'] = $day;
		$data['prev_day'] = date("n/j",strtotime("$year-$month-$day")-86400); 
		$data['next_day'] = date("n/j",strtotime("$year-$month-$day")+86400); 
		
		$this->load->view("home.html",$data);
	}
	
	function rss()
	{
		$this->load->model("history_model");
		header('Content-Type: text/xml; charset=utf-8');
		$year = intval(date("Y"));
		$month = intval(date("m"));
		$day = intval(date("d"));	
		$condition = "month='$month' and day='$day'";
		$hlist = $this->history_model->getList($condition);
		$rlist = array();
		foreach($hlist as $row)
		{
			$temp = array();
			$temp['title'] = $row->title;
			$temp['description'] = $row->description;
			$temp['link'] =  $this->config->item("base_url")."/{$row->month}/{$row->day}/{$row->id}.html";
			$rlist[] = $temp;
		}
		$data['rssitem'] = $rlist;
		$data['title'] = "{$month}月{$day}日  历史上的今天";
		$data['description'] = $day;
		$data['base_url'] =  $this->config->item("base_url");
		$data['link'] = $this->config->item("base_url");
		
		$this->load->view("rss.html",$data);
	}
		
	function hiii()
	{
		$this->load->model("history_model");
		$this->load->model("history_img_model");
		$month = intval(date("m"));
		$day = intval(date("d"));
		$condition = "month='$month' and day='$day'";
		$hlist = $this->history_model->getList($condition);
		$title = "";
		$description = "";
		$filename = "";
		$body = "";
		$id = 0;
		foreach($hlist as $info)
		{
			$id = $info->id;
			$title = $info->title;
			$description = $info->description;
			$condition = "hid='$id'";
			$img = $this->history_img_model->getInfo($condition);
			if($img->id>0)
			{
				$filename = $this->config->item("base_url")."/history/pic/$month/$day/".$img->filename;
				break;	
			}
			
		}
		
	
	}
	
	function get_calendar_html($year,$month,$day)
	{
		$month=intval($month);$day=intval($day);
		if($month==1)
		$prev_month = 12;
		else
		$prev_month = $month-1;
		
		if($month==12)
		$next_month = 1;
		else
		$next_month = $month+1;
		$this->load->helper('url');
		$prefs = array (
		               'show_next_prev'  => TRUE,
		               'next_prev_url'   => base_url()
		             );
		
		
		$prefs['template'] = '

   {table_open}<table id="wp-calendar" >{/table_open}

   {heading_row_start}<tr>{/heading_row_start}

   {heading_previous_cell}<th><a href="'.base_url()."$prev_month/$day".'"><img
					src="/images/left_arrow.gif" alt="" /></a></th>{/heading_previous_cell}
   {heading_title_cell}<th colspan="{colspan}"><a href="'.base_url()."$month/$day".'">{heading}</a></th>{/heading_title_cell}
   {heading_next_cell}<th><a href="'.base_url()."$next_month/$day".'"><img
					src="/images/right_arrow.gif" alt="" /></a></th>{/heading_next_cell}

   {heading_row_end}</tr>		<tfoot>
			<tr>
				<td colspan="3" id="prev" class="pad">&nbsp;</td>
				<td class="pad">&nbsp;</td>
				<td colspan="3" id="next" class="pad">&nbsp;</td>
			</tr>
		</tfoot>{/heading_row_end}
   {week_row_start}<tr id="week">{/week_row_start}
   {week_day_cell}<th abbr="" scope="col" title="">{week_day}</th></td>{/week_day_cell}
   {week_row_end}</tr>{/week_row_end}

   {cal_row_start}<tr>{/cal_row_start}
   {cal_cell_start}<td>{/cal_cell_start}

   {cal_cell_content}<a href="{content}">{day}</a>{/cal_cell_content}
   {cal_cell_content_today}<div class="highlight"><a href="{content}">{day}</a></div>{/cal_cell_content_today}

   {cal_cell_no_content}{day}{/cal_cell_no_content}
   {cal_cell_no_content_today}<div class="highlight">{day}</div>{/cal_cell_no_content_today}

   {cal_cell_blank}&nbsp;{/cal_cell_blank}

   {cal_cell_end}</td>{/cal_cell_end}
   {cal_row_end}</tr>{/cal_row_end}

   {table_close}</table>{/table_close}
';

		$this->load->library('calendar', $prefs);
		
		$cdata = array();
		for($i=1;$i<=31;$i++)
		{
			$cdata[$i] = $this->config->item("base_url")."/$month/$i";
		}		
		$html = $this->calendar->generate($year,$month,  $cdata);
		return $html;
	}	
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */